package be.bonamis.common.data;

import java.util.ArrayList;
import java.util.List;

/**
 * Gender enumeration.
 * @author FBonamis
 */
public enum Gender {

     /** Unknown Gender */
     Unknown(0),
     /** Male Gender */
     Male(1),
     /** Female gender */
     Female(2);

     // Properties
     /** Gets the ISO 5218 code. */
     private final int code;
     /** active or not */
     private final boolean active;
     // End Properties

     // Constructors
     /**
      * Default constructor
      * @param code
      *             gender iso code
      */
     private Gender(final int code) {
          this(code, true);
     }

     /**
      * Default constructor
      * @param code
      *             gender iso code
      * @param active
      *             true to specify the gender is active, false otherwise.
      */
     private Gender(final int code, boolean active) {
          this.code = code;
          this.active = active;
     }
     // End constructors

     // Getters

     /**
      * This method selects the default code.
      * @return the ISO 5218 code
      */
     public int getCode() {
          return code;
     }
     /**
      * @return true is the gender is active otherwise false is returned.
      */
     public boolean isActive() {
          return active;
     }
     
     // End Getters

     // Static methods
     
     /**
      * Gets a list of all the genders defined.
      * @return An array of all genders.
      */
     public static Gender[] getAllGenders() {
          return Gender.values();
     }

     /**
      * Gets a list of all the active genders defined.
      * @return An array of all the active genders
      */
     public static Gender[] getActiveGenders() {
          List<Gender> activeGenders = new ArrayList<Gender>();
          for (Gender gender : Gender.getAllGenders()) {
               if (gender.active) {
                    activeGenders.add(gender);
               }
          }
          Gender[] result = new Gender[activeGenders.size()];
          return activeGenders.toArray(result);
     }
     
     /**
      * Looks up a gender by the iso code.
      * @param code
      *             The gender iso code
      * @return the gender corresponding with the code.
      * @throws IllegalArgumentException
      *              if the gender is not found based on the code.
      */
     public static Gender getGender(int code) {
          Gender gender = tryGetGender(code);
          if (gender == null) {
               throw new IllegalArgumentException("Gender " + code
                         + " not valid [The specificed gender code does not exist].");
          }
          return gender;
     }

     /**
      * Looks up a gender by the iso code.
      * @param code
      *             The gender iso code
      * @return the gender corresponding with the code or null if no gender is found.
      */
     public static Gender tryGetGender(int code) {
          for (Gender gender : Gender.getAllGenders()) {
               if (code == gender.code) {
                    return gender;
               }
          }
          return null;
     }

}
